package test

import (
	"chainserver/env"
	"chainserver/models"
	"log"
	"path/filepath"
	"runtime"

	"github.com/astaxie/beego"
)

func Init(initDB bool) (closeFunc func()) {
	_, path, _, _ := runtime.Caller(1)
	dir := filepath.Dir(filepath.Join(path, "../../"))
	beego.TestBeegoInit(dir)

	if err := env.LoadConfig(); err != nil {
		log.Panic("加载配置文件失败，err:", err.Error())
		return
	}
	if initDB {
		if err := models.InitDB(); err != nil {
			log.Panic("初始化数据库失败，err:", err.Error())
		}
		closeFunc = func() {
			models.CloseDB()
		}
	}
	return
}
